﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DTO;
using BUS;
using System.Data;
using System.IO;

namespace EC21_EDiamond
{
    public partial class CapNhatSanPham : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (!Page.IsPostBack)
            {
                int masp = int.Parse(Request.QueryString["id"].ToString());
                LoadSanPhamTheoMa(masp);
            }
        }

        //Hien thi thong tin san pham theo ma
        private void LoadSanPhamTheoMa(int ma)
        {
            SanPhamBUS spBUS = new SanPhamBUS();
            DataTable dt = spBUS.LoadDanhSachSanPhamTheoMa(ma);
            txtTenSP.Text = dt.Rows[0][0].ToString();
            txtMoTa.Text = dt.Rows[0][1].ToString();
            txtLoaiDa.Text = dt.Rows[0][2].ToString();
            txtTrongLuong.Text = dt.Rows[0][3].ToString();
            txtChatLieu.Text = dt.Rows[0][4].ToString();
            txtGiaGoc.Text = dt.Rows[0][5].ToString();
            txtGiaKM.Text = dt.Rows[0][6].ToString();
            txtSoLuongTon.Text = dt.Rows[0][7].ToString();
            cbxDanhMuc.SelectedValue = dt.Rows[0][8].ToString();
            cbxThuongHieu.SelectedValue = dt.Rows[0][9].ToString();
            imgHinhAnh.ImageUrl = dt.Rows[0][10].ToString();

            if (dt.Rows[0][12].ToString() == "1")
            {
                cbTopSP.Checked = true;
            }
            else
            {
                cbTopSP.Checked = false;
            }
        }

        //Cap nhat thong tin san pham 
        protected void btnDongY_Click(object sender, EventArgs e)
        {
            SanPhamDTO spDTO = new SanPhamDTO();
            int masp = int.Parse(Request.QueryString["id"].ToString());
            spDTO.MaSP = masp;
            spDTO.TenSP = txtTenSP.Text;
            spDTO.Mota = txtMoTa.Text;
            spDTO.Loaida = txtLoaiDa.Text;
            spDTO.Trongluong = float.Parse(txtTrongLuong.Text);
            spDTO.Chatlieu = txtChatLieu.Text;
            spDTO.Giagoc = int.Parse(txtGiaGoc.Text);
            if (txtGiaKM.Text == "")
            {
                spDTO.GiaKM = int.Parse("");
            }
            else
            {
                spDTO.GiaKM = int.Parse(txtGiaKM.Text);
            }
            spDTO.SlTon = int.Parse(txtSoLuongTon.Text);
            spDTO.MaDM = int.Parse(cbxDanhMuc.SelectedValue.ToString());
            spDTO.MaTH = int.Parse(cbxThuongHieu.SelectedValue.ToString());
            if (cbTopSP.Checked == true)
            {
                spDTO.IsIndex = 1;
            }
            else
            {
                spDTO.IsIndex = 0;
            }
            if (upAnh.HasFile)//neu chon 1 anh khac thi lay duong dan cua anh moi
            {
                string filename = Path.GetFileName(upAnh.FileName);
                if (CheckFileType(filename))
                {
                    upAnh.SaveAs(Server.MapPath("./Content/fontend/img/") + filename);
                    spDTO.HinhAnh = "./Content/fontend/img/" + filename;
                }
                else
                {
                    lbErrorImg.Visible = true;
                    lbErrorImg.Text = "Vui lòng chọn đúng định dạng ảnh";
                    return;
                }
            }
            else//nguoc lai lay duong dan cua anh cu
            {
                spDTO.HinhAnh = imgHinhAnh.ImageUrl;
            }

            SanPhamBUS spBUS = new SanPhamBUS();
            if (spBUS.CapNhatSanPham(spDTO) == 1)//cap nhat thanh cong
            {
                Response.Redirect("CapNhatSanPham.aspx?id=" + masp);
                lbThongBaoKQCapNhat.Visible = true;  
                lbThongBaoKQCapNhat.Text = "Cập nhật thành công";
            }
        }

        //Kiem tra dinh dang co dung la anh ko
        private bool CheckFileType(string FileName)
        {
            string ext = Path.GetExtension(FileName);
            if (ext.Equals(".png") || ext.Equals(".jpg") || ext.Equals(".gif"))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        protected void cbxDanhMuc_SelectedIndexChanged(object sender, EventArgs e)
        {
            cbxDanhMuc.AutoPostBack = true;
        }

        protected void cbxThuongHieu_SelectedIndexChanged(object sender, EventArgs e)
        {
            cbxThuongHieu.AutoPostBack = true;
        }

        //Huy khong cap nhat thi load lai thong tin san pham cu
        protected void btnReset_Click(object sender, EventArgs e)
        {
            int masp = int.Parse(Request.QueryString["id"].ToString());
            Response.Redirect("CapNhatSanPham.aspx?id=" + masp);
        }

        
    }
}